﻿using ConfExpert.Utility;
using DevTeam;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;

namespace ConfExpert.Forms.Araclar
{
    public partial class EmailSender : XPopupForm
    {
        int pos = 0;
        public EmailSender(string to, string cc, string subject, string message, int subjectPosition = 0)
        {
            InitializeComponent();
            txtTo.Text = to;
            txtSubject.Text = subject;
            txtMessage.HtmlText = message;
            txtCC.Text = cc;
            pos = subjectPosition;
            gleUsers.Properties.Assign(RI(Rp.V_User));
        }

        byte[] File = null;
        string FileName = "";
        string ReplyTo = "";

        public EmailSender(string to, string cc, string subject, string message, byte[] file, string fileName, string replyTo)
        {
            InitializeComponent();
            txtTo.Text = to;
            txtSubject.Text = subject;
            txtMessage.HtmlText = message;
            txtCC.Text = cc;
            this.File = file;
            this.FileName = fileName;
            this.ReplyTo = replyTo;
            gleUsers.Properties.Assign(RI(Rp.V_User));

        }


        private void ExceptionYanitla_Load(object sender, EventArgs e)
        {
            ShowOnlyFooter();
            //btnSend.Click += btnSend_Click;
            this.Shown += EmailSender_Shown;
        }

        void EmailSender_Shown(object sender, EventArgs e)
        {
            txtMessage.Focus();

            DevExpress.XtraRichEdit.API.Native.DocumentPosition position = txtMessage.Document.CreatePosition(pos);
            txtMessage.Document.CaretPosition = position;
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            string message = txtMessage.HtmlText;
            if (File == null)
                Mail.Send(txtTo.Text, txtSubject.Text, message, txtCC.Text, ReplyTo);
            else
                Mail.Send(txtTo.Text, txtSubject.Text, message, txtCC.Text, File, FileName, ReplyTo);

            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void btnAddCC_Click(object sender, EventArgs e)
        {
            List<string> ccs = txtCC.Text.Split(",".ToCharArray()).ToList();
            DataRow dr = ((DataRowView)gleUsers.GetSelectedDataRow()).Row;
            string email = dr["email"].ToString();

            if (!ccs.Contains(email))
                ccs.Add(email);

            txtCC.Text = String.Join(",", ccs);
        }
    }
}
